model = function(){}
//加载栏目属性
model.ready = function(){
	var html = Acan.html.tag('table',{'class':'-table'},
		Acan.html.tag('tr',{},
			Acan.html.tag('td',{},Acan.html.tag('input',{type:'button',onclick:"model.newadd()",value:'添加栏目'}))
			+Acan.html.tag('td',{id:'model_add_type'})
		)
	);
	html += Acan.html.tag('table',{id:'model_add_con','class':'-table'});//栏目编辑
	jQuery("#model_add").append(html);
}
model.newadd = function(){
	data = {text:'文本类型',textarea:'多行文本输入框',radio:'单选框',checkbox:'复选框',select:'下拉列表'};
	var html = Acan.html.inputs({type:'radio',name:'radio',onclick:'model.type(this)'},data,'','请选择添加类型:');
	jQuery("#model_add_type").html(html);
}
model.type = function(obj){
	var html = '';
	jQuery("#model_add_con").html("");
	html = Acan.html.tag('tr',{},
		Acan.html.tag('input',{type:'hidden','class':'madd_type',value:obj.value})
		+Acan.html.tag('td',{},Acan.html.tag('input',{type:'text','class':'madd_type_id'},'类型ID:'))
		+Acan.html.tag('td',{},Acan.html.tag('input',{type:'text','class':'madd_label'},'名称:'))
		+Acan.html.tag('td',{},Acan.html.tag('input',{type:'text','class':'madd_name'},'name:'))
		+Acan.html.tag('td',{},Acan.html.tag('input',{type:'text','class':'madd_order',size:'3',value:Acan.count(mdata)},'排序:'))
	)
	+function(){
		if(obj.value != 'text' && obj.value != 'textarea' ){
			return Acan.html.tag('tr',{},Acan.html.tag('td',{colspan:"10"},Acan.html.tag('input',{type:'text','class':'madd_data',size:'100'},'数据:')));
		}else{
			return '';
		}
	}()
	+Acan.html.tag('tr',{},
		Acan.html.tag('td',{colspan:"10",align:'center'},
			Acan.html.tag('input',{type:'button','class':'madd_btn_add',onclick:'model.save()',value:'添加'})
		)
	);
	jQuery("#model_add_con").append(html);
}
model.save = function(id){
	var arr = Object();
	arr.attr = Object();
	arr.attr.type = jQuery('.madd_type').val();
	arr.attr.name = jQuery('.madd_name').val();
	arr.label = jQuery('.madd_label').val();
	arr.type_id = jQuery('.madd_type_id').val();
	arr.order = jQuery('.madd_order').val();
	var data = jQuery('.madd_data').val();
	if(data){
		arr.data = Object();
		arr.data = Acan.base.str_obj(data);
	}
	if(typeof(id) == 'undefined'){
		var id = Acan.count(mdata);
	}
	mdata[id] = arr;
	model.list_refesh();
}
model.del = function(id){
	delete mdata[id];
	model.list_refesh();
}
model.edit = function(id){
	model.newadd();
	var type = mdata[id].attr.type;
	jQuery('#model_add_type input[value="'+type+'"]').click();
	jQuery('.madd_type_id').val(mdata[id].type_id);
	jQuery('.madd_name').val(mdata[id].attr.name);
	jQuery('.madd_label').val(mdata[id].label);
	var order = id;
	if(mdata[id].order){
		order = mdata[id].order;
	}
	jQuery('.madd_order').val(order);
	jQuery('.madd_data').val(Acan.base.obj_str(mdata[id].data));
	jQuery('.madd_btn_add').remove();
	if(jQuery('.madd_btn_up').val()){return;}
		
	var html = Acan.html.tag('tr',{},
		Acan.html.tag('td',{colspan:"10",align:'center'},
			Acan.html.tag('input',{type:'button','class':'madd_btn_up',onclick:'model.save('+id+')',value:'保存'})
		)
	);
	jQuery("#model_add_con").append(html);
}
model.ajax_save = function(id){
	if(typeof(id) == 'undefined'){id = '';}
	var save = jQuery("#save_form").serialize();
	if(typeof(mdata) == 'object')
		save += "&save[value]="+Acan.base.obj_json(mdata);
	jQuery.ajax({
		type: "POST",
		url: Aurl.Amodr+"-save-"+id,
		data: save,
		beforeSend: function(){Acan.alert('开始请求');},
		ajaxSend: function(){Acan.alert('发送请求');},
		success: function(data){
			Acan.alert(data,1000);
		}
	})
}
//加载栏目列表
model.list = function(){
	var html = '';
	if(mdata[0]){
		html += Acan.html.tag('tr',{},Acan.html.tag('th',{colspan:'10',align:'center'},'栏目列表'));
		html += Acan.html.tag('tr',{},
			Acan.html.tag('th',{},'类型ID')
			+Acan.html.tag('th',{},'表单')
			+Acan.html.tag('th',{},'排序')
			+Acan.html.tag('th',{},'操作')
		);
		for(var x in mdata){
			html += Acan.html.tag('tr',{},
				Acan.html.tag('td',{},mdata[x].type_id)
				+Acan.html.tag('td',{},
					function(){
						var type = mdata[x].attr.type;
						if(type == 'text')
							return Acan.html.tag('input',mdata[x].attr,mdata[x].label);
						else if(type == 'textarea')
							return Acan.html.tag('textarea',mdata[x].attr,mdata[x].label);
						else if(type == 'select')
							return Acan.html.select('select',mdata[x].data,'',mdata[x].label);
						else if(Acan.base.in_array(type,['radio','checkbox']))
							return Acan.html.inputs(mdata[x].attr,mdata[x].data,'',mdata[x].label);
					}()
				)
				+Acan.html.tag('td',{},mdata[x].order)//排序
				+Acan.html.tag('td',{},
					Acan.html.tag('input',{type:'button',onclick:'model.edit('+x+')',value:'编辑'})
					+Acan.html.tag('input',{type:'button',onclick:'model.del('+x+')',value:'删除'})
				)//操作按钮
			);
		}
	}
	var id = jQuery('.model_form_id').val();
	if(id == ''){
		var btn_text = '添加';
	}else{
		var btn_text = '更新';
	}
	html += Acan.html.tag('tr',{},
		Acan.html.tag('td',{colspan:'10',align:'center'},
			Acan.html.tag('input',{type:'button',onclick:'model.ajax_save('+id+')',value:btn_text})
			+Acan.html.tag('input',{type:'button',onclick:'Acan.url.back()',value:'返回'})
		)//操作按钮
	)
	html = Acan.html.tag('table',{'class':'-table'},html);//操作按钮
	jQuery("#model_list").append(html);
}
model.list_refesh = function(){
	if(Acan.get('#model_add')){
		jQuery('#model_add').html("");
		model.ready();
	}
	if(Acan.get('#model_list')){
		jQuery('#model_list').html("");
		model.list();
	}
}
jQuery(document).ready(function(){
	model.list_refesh();
});